package com.ujn.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ujn.zhxy.pojo.Teacher;
import com.ujn.zhxy.service.TeacherService;
import com.ujn.zhxy.util.MD5;
import com.ujn.zhxy.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //注意 不是@Controller ，异步交互的方法
@RequestMapping("/sms/teacherController")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;
    ///getTeachers/1/3?name=1&clazzName=

    @ApiOperation("分页带条件查询教师信息")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(
            @PathVariable("pageNo") @ApiParam("页码数") Integer pageNo,
            @PathVariable("pageSize") @ApiParam("页大小") Integer pageSize,
            @ApiParam("教师名") Teacher teacher
    ){
        Page<Teacher> pageParam = new Page<>(pageNo,pageSize);
        IPage<Teacher> teacherRs = teacherService.getTeacherByOpr(pageParam,teacher);
        return Result.ok(teacherRs);
    }

    @ApiOperation("添加或修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(
            @ApiParam("json格式的teacher") @RequestBody Teacher teacher
    ){
        //如果是新增的话要对密码加密
        Integer id = teacher.getId();
        if (null == id || 0 == id) {
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    @ApiOperation("删除单个或多个教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(
            @ApiParam("要删除的教师ids，json格式的") @RequestBody List<Integer> ids
    ){
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
